#include "lrcpage.h"
#include "ui_lrcpage.h"


LrcPage::LrcPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LrcPage)
{

    ui->setupUi(this);

    this->setWindowFlag(Qt::FramelessWindowHint);

    ui->hideBtn->setIcon(QIcon(":/images/xiala.png"));

    animation = new QPropertyAnimation(this, "geometry", this);
    animation->setDuration(250);
    animation->setStartValue(QRect(10, 10, width(), height()));
    animation->setEndValue(QRect(10, 10 + height(), width(), height()));
    //点击按钮显示动画
    connect(ui->hideBtn, &QPushButton::clicked, [=](){
        animation->start();
        //this->hide();
    });

    //动画结束，隐藏窗口
    connect(animation, &QPropertyAnimation::finished, [=](){
            this->hide(); // 隐藏窗口
    });

}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrc(const QString &lrcPath)
{
    lrcLines.clear();

    QFile file(lrcPath);
    if(!file.open(QFile::ReadOnly)){
        qDebug()<<"打开⽂件:"<<lrcPath;
        return false;
    }

    while(!file.atEnd())
    {
        QString lrcWord = file.readLine(1024);
        // [00:17.94]那些失眠的⼈啊 你们还好吗
        // [0:58.600.00]你像⼀只⻜来⻜去的蝴蝶
        int left = lrcWord.indexOf('[');
        int right = lrcWord.indexOf(']');
        // 解析时间
        qint64 lineTime = 0;
        int start = 0;
        int end = 0;
        QString time = lrcWord.mid(left, right-left+1);
        // 解析分钟
        start = 1;
        end = time.indexOf(':');
        lineTime += lrcWord.mid(start, end-start).toInt()*60*1000;
        // 解析秒
        start = end + 1;
        end = time.indexOf('.', start);
        lineTime += lrcWord.mid(start, end-start).toInt()*1000;
        // 解析毫秒
        start = end+1;
        end = time.indexOf('.', start);
        lineTime += lrcWord.mid(start, end-start).toInt();
        // 解析歌词
        QString word = lrcWord.mid(right+1).trimmed();
        lrcLines.push_back(lyricLine(lineTime, word.trimmed()));
    }

    // for(auto word : lrcLines)
    // {
    //     qDebug()<<word._time<<" "<<word._text;
    // }
    return true;
}

//根据时间获取行号
int LrcPage::getLineLrcWordIndex(qint64 time)
{
    // 如果歌词是空的，返回-1
    if(lrcLines.isEmpty())
    {
        return -1;
    }
    if(lrcLines[0]._time > time)
    {
        return 0;
    }
    // 通过时间⽐较，获取下标
    for(int i = 1; i < lrcLines.size(); ++i)
    {
        if(time > lrcLines[i-1]._time && time <= lrcLines[i]._time)
        {
            return i-1;
        }
    }
    // 如果没有找到，返回最后⼀⾏
    return lrcLines.size()-1;
}

//根据行号获取内容
QString LrcPage::getLineWord(qint64 index)
{
    if(index < 0 || index >= lrcLines.size()){
        return "";
    }

    return lrcLines[index]._text;
}

//将内容显示到页面上
void LrcPage::showLrcWord(int time)
{
    int index = getLineLrcWordIndex(time);
    if(-1 == index)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("当前歌曲⽆歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }else
    {
        ui->line1->setText(getLineWord(index-3));
        ui->line2->setText(getLineWord(index-2));
        ui->line3->setText(getLineWord(index-1));
        ui->lineCenter->setText(getLineWord(index));
        ui->line4->setText(getLineWord(index+1));
        ui->line5->setText(getLineWord(index+2));
        ui->line6->setText(getLineWord(index+3));
    }
}

//修改页面上的歌名
void LrcPage::setMusicName(const QString &name)
{
    ui->musicName->setText(name);
}

//修改页面上的歌手名
void LrcPage::setSingerName(const QString &name)
{
    ui->singerName->setText(name);
}



